/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.manager;

import br.com.deployment.dao.RegistrosDao;
import br.com.deployment.dao.TransferenciaDao;
import br.com.deployment.entidade.registroimovel.Imovel;
import br.com.deployment.entidade.registroimovel.Transferencia;
import br.com.deployment.entidade.acesso.Usuario;
import br.com.deployment.entidade.registroimovel.Registro;
import br.com.deployment.message.Msg;
import br.com.deployment.servico.ImovelServico;
import br.com.deployment.servico.RegistroServico;
import br.com.deployment.servico.TransferenciaServico;
import br.com.deployment.servico.UsuarioServico;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author elyjr
 */
@ManagedBean
@SessionScoped
public class ManagerRegistro implements Serializable{

    @EJB
    RegistroServico registrosDao;
    
    @EJB
    TransferenciaServico transferenciaDao;
            
    @EJB
    ImovelServico imovelDao;
    
        
    @EJB
    UsuarioServico usuarioDao;
    
    private Usuario usuario;
    private Registro registro;
    private Imovel imovel;
    private Transferencia transferencia;
    
    public ManagerRegistro() {

    }
    
    @PostConstruct
    public void init(){
        this.registro = new Registro();
        this.transferencia = new Transferencia();
        this.usuario = new Usuario();
    }

    public Usuario getUsuario() {
        return usuario;
    }

    public void setUsuario(Usuario usuario) {
        this.usuario = usuario;
    }

    public Registro getRegistro() {
        return registro;
    }

    public void setRegistro(Registro registro) {
        this.registro = registro;
    }
    
    

    public Transferencia getTransferencia() {
        return transferencia;
    }

    public void setTransferencia(Transferencia transferencia) {
        this.transferencia = transferencia;
    }

    
    

    public Imovel getImovel() {
        return imovel;
    }

    public void setImovel(Imovel imovel) {
        this.imovel = imovel;
    }
    

    
    public void salvar(){
        try {
            usuario = usuarioDao.verificarUsuarioNome(FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal().getName());
            
            List<Registro> lstRegistros = new ArrayList<Registro>();
            int i = 0;
            i = registrosDao.findUltima(imovel) == null ? 0 : registrosDao.findUltima(imovel).getNumero();
            
            registro.setNumero(i + 1);
            registro.setDataRegistro(new Date());
            registro.setUsuario(usuario);
            
            Msg.messagemInfo("Salvo com Sucesso !");
            
            lstRegistros = registrosDao.getRegistros(imovel);
            lstRegistros.add(registro);
            imovel.setRegistros(lstRegistros);            
            imovelDao.edit(imovel);
            reinit();
            
            
        } catch (Exception ex) {
            Logger.getLogger(ManagerRegistro.class.getName()).log(Level.SEVERE, null, ex);
            Msg.messagemError("Erro ao Salvar!");
        }
    }
    
    public void reinit(){
        registro = new Registro();
    }
     public void registrarImovel(Imovel imovelParam){
          imovel = imovelDao.find(imovelParam.getId());;
          
          transferencia = transferenciaDao.verificaPrimeiraTrans(imovelParam.getId());
         
          redirect("criarRegistro.xhtml");         
     }
     
       
     public void redirect(String string) {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            context.getExternalContext().redirect(string);
        } catch (IOException ex) {
            Logger.getLogger(ManagerImovel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    
}
